//=============================================================================
// ProMesh.NET - .NET Web Application Framework 
//
// Copyright (c) 2003-2008 Philippe Leybaert
//
// Permission is hereby granted, free of charge, to any person obtaining a copy 
// of this software and associated documentation files (the "Software"), to deal 
// in the Software without restriction, including without limitation the rights 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is 
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//=============================================================================

using System;
using Activa.LazyParser;

namespace Activa.ProMesh
{
    internal class TemplateParserContext : CSharpContext
    {
        private readonly ViewContext _viewContext;
        private readonly Template _template;

        private void Initalize()
        {
            NonEmptyStringIsTrue = true;
            NotNullIsTrue = true;
            NullIsFalse = true;
            EmptyStringIsFalse = true;
            NotNullIsTrue = true;
            NotZeroIsTrue = true;
            EmptyCollectionIsFalse = true;
        }

        public TemplateParserContext(ViewContext viewContext, Template template)
        {
            Initalize();

            _viewContext = viewContext;
            _template = template;
        }

        public TemplateParserContext(TemplateParserContext parentContext, Template template) : base(parentContext)
        {
            Initalize();

            _viewContext = parentContext.ViewContext;
            _template = template;
        }

        public TemplateParserContext(TemplateParserContext parentContext) : base(parentContext)
        {
            Initalize();

            _viewContext = parentContext.ViewContext;
            _template = parentContext.Template;
        }

        public override bool Get(string varName, out object value, out Type type)
        {
            if (base.Get(varName, out value, out type))
                return true;

            if (ViewContext.ViewData.TryGetValue(varName, out value, out type))
                return true;

            if (ViewContext.Controls[varName] != null)
            {
                value = ViewContext.Controls[varName];
                type = ViewContext.Controls[varName].GetType();

                return true;
            }

            value = null;
            type = typeof (object);

            return false;
        }

        public override bool Exists(string varName)
        {
            return ViewContext.ViewData.ContainsKey(varName) || ViewContext.Controls[varName] != null || base.Exists(varName);
        }

        public override IParserContext CreateLocal()
        {
            return new TemplateParserContext(this);
        }

        public Template Template
        {
            get { return _template; }
        }

        public ViewContext ViewContext
        {
            get { return _viewContext; }
        }
    }
}